package cn.learn.service.system.user.impl;

import cn.learn.dao.module.ModuleDao;
import cn.learn.dao.system.user.UserDao;
import cn.learn.domain.module.Module;
import cn.learn.domain.system.user.User;
import cn.learn.service.system.user.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;
    @Autowired
    private ModuleDao moduleDao;

    /**
     * 分页查询用户数据
     * @param pageNum
     * @param pageSize
     * @param companyId
     * @return
     */
    @Override
    public PageInfo selectAllByPage(int pageNum, int pageSize, String companyId) {
        PageHelper.startPage(pageNum, pageSize);
        List<User> resultUser = userDao.selectAllByPage(companyId);
        PageInfo resultData = new PageInfo(resultUser);
        return resultData;
    }

    /**
     * 添加一条用户
     * @param user
     */
    @Override
    public void addOne(User user) {
        userDao.addOne(user);
    }

    /**
     * 根据id查询一条数据
     * @param user
     * @return
     */
    @Override
    public User findOneById(User user) {
        User resultData = userDao.findOneById(user);
        return resultData;
    }

    /**
     * 根据id修改一条数据
     * @param user
     */
    @Override
    public void updateOneByID(User user) {
        userDao.updateOneById(user);
    }

    /**
     * 根据id删除一条用户
     * @param id
     */
    @Override
    public void deleteById(String id) {
        userDao.deleteById(id);
    }

    /**
     * 根据邮箱查询用户是否存在
     * @param email
     * @return
     */
    @Override
    public User findByEmail(String email) {
        User user = userDao.selectByEmail(email);
        return user;
    }

    /**
     * 判断用户权限，获取对应的菜单
     * @param user
     * @return
     */
    @Override
    public List<Module> findMenus(User user) {
        List<Module> resultModule = null;
        if (user.getDegree() == 0) {
            resultModule = moduleDao.findUserMenus(0);
        } else if (user.getDegree() == 1) {
            resultModule = moduleDao.findUserMenus(1);
        } else {
            // 普通员工，涉及五张表
            resultModule = moduleDao.findUserMenusToRBAC(user.getId());
        }
        return resultModule;
    }
    @Override
    public User findByWxOpenId(String openId) {
        return     userDao.findUserByOpenId(openId);
    }
}
